package com.lz.comm.widget

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.animation.Animation.AnimationListener

/**
 *  author: you : 2020/11/21
 */
class ShadowView : View {

    private val shadowColor = Color.parseColor("#80000000")
    private val showAnim by lazy {
        AlphaAnimation(0.1f, 1.0f).apply {
            duration = ANIM_DURATION
            setAnimationListener(ShadowAnimationListener())
        }
    }
    private val hideAnim by lazy {
        AlphaAnimation(1.0f, 0.1f).apply {
            duration = ANIM_DURATION
            setAnimationListener(ShadowAnimationListener(false))
        }
    }

    constructor(context: Context?) : super(context) {
        initView()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        initView()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initView()
    }

    private fun initView() {
        setBackgroundColor(shadowColor)
        visibility = INVISIBLE
    }

    /**
     * 显示
     */
    fun show() {
        clearAnimation()
        startAnimation(showAnim)
    }

    /**
     * 隐藏
     */
    fun hide() {
        clearAnimation()
        startAnimation(hideAnim)
    }

    companion object {
        const val ANIM_DURATION = 200L
    }

    private inner class ShadowAnimationListener(val showShadow: Boolean = true) : AnimationListener {

        override fun onAnimationStart(animation: Animation) = Unit

        override fun onAnimationEnd(animation: Animation) {
            visibility = if (showShadow) VISIBLE else INVISIBLE
        }

        override fun onAnimationRepeat(animation: Animation) = Unit
    }

}